package com.huluobo.a2207parkdemo.parking

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2207parkdemo.R
import com.huluobo.a2207parkdemo.adapter.ParkingAdapter
import com.huluobo.a2207parkdemo.databinding.ActivityParkingBinding
import com.huluobo.base.bean.ParkingBean
import com.huluobo.base.bean.ParkingBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class ParkingActivity : BaseActivity<ActivityParkingBinding, ParkingViewModel>() {
    private lateinit var parkingAdapter: ParkingAdapter
    private lateinit var parkingBeanItem: ParkingBeanItem
    override fun initData() {
        vm.getAllParkingSuccess.observe(this) {
            val list = Gson().fromJson(it, ParkingBean::class.java)
            parkingAdapter.data.clear()
            parkingAdapter.data.addAll(list)
            parkingAdapter.notifyDataSetChanged()
        }

        vm.deleteParkingSuccess.observe(this) {
            ToastUtils.showLong("删除车位成功")
            parkingAdapter.remove(parkingBeanItem)
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getAllParking()
    }

    override fun initView() {
        binding.parkingHeader.headerTitle.text = "车位申请"
        binding.parkingHeader.headerBack.setOnClickListener { finish() }

        binding.parkingRv.layoutManager = LinearLayoutManager(this)
        parkingAdapter = ParkingAdapter()
        binding.parkingRv.adapter = parkingAdapter

        parkingAdapter.setOnItemClickListener { adapter, view, position ->
            parkingBeanItem = parkingAdapter.data[position]

            val intent = Intent(this, ParkingVRActivity::class.java)
            intent.putExtra(Const.PARAM_PARKING_BEAN, parkingBeanItem)
            startActivity(intent)
        }

        parkingAdapter.setOnItemLongClickListener { _, _, position ->
            AlertDialog.Builder(this)
                .setTitle("删除车位?")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定") { _, _ ->
                    parkingBeanItem = parkingAdapter.data[position]
                    val map = mutableMapOf<String, Int>()
                    map[Const.PARAM_PARKING_ID] = parkingAdapter.data[position].parkId
                    val json = Gson().toJson(map)
                    val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                    vm.deleteParking(body)
                }.show()

            true
        }
    }
}